package com.ibeeking.found.cop.b.rest.service.mq;

import com.ibeeking.found.common.constants.MqTopic;
import com.ibeeking.found.common.entity.MqMsgEventParam;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.rocketmq.annotation.MQConsumer;
import com.ibeeking.nematos.rocketmq.base.AbstractMQPushConsumer;
import com.ibeeking.nematos.utils.json.JsonUtils;
import me.chanjar.weixin.cp.constant.WxCpConsts;
import me.chanjar.weixin.cp.constant.WxCpTpConsts;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@MQConsumer(topic = MqTopic.WX_MSG_EVENT_TOPIC, consumerGroup = MqTopic.WX_MSG_EVENT_GROUP)
public class CopWxCpMsgEventConsumer extends AbstractMQPushConsumer<MqMsgEventParam> {

    @Resource
    private CopContactMqMsgEventHandler copContactMqMsgEventHandler;

    @Resource
    private CopFriendsMqMsgEventHandler copFriendsMqMsgEventHandler;

    @Resource
    private CopFriendsTagsMqMsgEventHandler copFriendsTagsMqMsgEventHandler;

    @Resource
    private CopDataTalkMqMsgEventHandler copDataTalkMqMsgEventHandler;

    @Resource
    private CopGroupMqMsgEventHandler copGroupMqMsgEventHandler;

    @Override
    public ConsumeConcurrentlyStatus dealMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
        for (MessageExt messageExt : list) {
            MqMsgEventParam mqParam = parseMessage(messageExt);
            // parse ext properties
            Map<String, Object> ext = parseExtParam(messageExt);
            if (null != mqParam) {
                CopWxCpMsgEventConsumer mqConsumer = SpringBeanUtil.getBean(CopWxCpMsgEventConsumer.class);
                boolean result = mqConsumer.process(mqParam, ext);
                if(!result) {
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }else{
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            }
        }
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }

    @Override
    public boolean process(MqMsgEventParam mqParam, Map<String, Object> extMap) {
        LogUtils.info(">>>>>>> GetMQMsg param:{}", JsonUtils.toJsonStr(mqParam));
        String msgType = mqParam.getMsgType();
        String event = mqParam.getEvent();
        String changeType = mqParam.getChangeType();
        String tenantId = mqParam.getTenantId();
        //同步部门列表
        if(WxCpTpConsts.InfoType.CHANGE_CONTACT.equalsIgnoreCase(event)) {
            if (WxCpConsts.ContactChangeType.CREATE_PARTY.equalsIgnoreCase(changeType)) {
                return copContactMqMsgEventHandler.addDepartmentMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ContactChangeType.UPDATE_PARTY.equalsIgnoreCase(changeType)){
                return copContactMqMsgEventHandler.updateDepartmentMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ContactChangeType.DELETE_PARTY.equalsIgnoreCase(changeType)){
                return copContactMqMsgEventHandler.deleteDepartmentMsgEvent(mqParam, extMap);
            }
            if (WxCpConsts.ContactChangeType.CREATE_USER.equalsIgnoreCase(changeType)) {
                return copContactMqMsgEventHandler.addEmployeeMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ContactChangeType.UPDATE_USER.equalsIgnoreCase(changeType)){
                return copContactMqMsgEventHandler.updateEmployeeMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ContactChangeType.DELETE_USER.equalsIgnoreCase(changeType)){
                return copContactMqMsgEventHandler.deleteEmployeeMsgEvent(mqParam, extMap);
            }
        }
        if(WxCpConsts.EventType.CHANGE_EXTERNAL_TAG.equalsIgnoreCase(event)){
            if(WxCpConsts.ExternalTagChangeType.CREATE.equalsIgnoreCase(changeType)){
                copFriendsTagsMqMsgEventHandler.addFriendsTagsMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalTagChangeType.UPDATE.equalsIgnoreCase(changeType)){
                copFriendsTagsMqMsgEventHandler.updateFriendsTagsMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalTagChangeType.DELETE.equalsIgnoreCase(changeType)){
                copFriendsTagsMqMsgEventHandler.deleteFriendsTagsMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalTagChangeType.SHUFFLE.equalsIgnoreCase(changeType)){
                copFriendsTagsMqMsgEventHandler.shuffleFriendsTagsMsgEvent(mqParam, extMap);
            }
        }
        if(WxCpConsts.EventType.CHANGE_EXTERNAL_CONTACT.equalsIgnoreCase(event)){
            if(WxCpConsts.ExternalContactChangeType.ADD_EXTERNAL_CONTACT.equalsIgnoreCase(changeType)){
                copFriendsMqMsgEventHandler.addFriendsMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalContactChangeType.EDIT_EXTERNAL_CONTACT.equalsIgnoreCase(changeType)){
                copFriendsMqMsgEventHandler.updateFriendsMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalContactChangeType.DEL_EXTERNAL_CONTACT.equalsIgnoreCase(changeType)){
                copFriendsMqMsgEventHandler.deleteFriendsMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalContactChangeType.DEL_FOLLOW_USER.equalsIgnoreCase(changeType)){
                copFriendsMqMsgEventHandler.deleteFriendsFollowMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalContactChangeType.ADD_HALF_EXTERNAL_CONTACT.equalsIgnoreCase(changeType)){
                copFriendsMqMsgEventHandler.addFriendsHalfMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalContactChangeType.TRANSFER_FAIL.equalsIgnoreCase(changeType)){
                copFriendsMqMsgEventHandler.tansferFailMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.MsgAuditChangeType.MSG_AUDIT_APPROVED.equalsIgnoreCase(changeType)){
                copDataTalkMqMsgEventHandler.auditApproveMsgEvent(mqParam, extMap);
            }
        }
        if(WxCpConsts.EventType.CHANGE_EXTERNAL_CHAT.equalsIgnoreCase(event)){
            if(WxCpConsts.ExternalChatChangeType.CREATE.equalsIgnoreCase(changeType)){
                copGroupMqMsgEventHandler.addGroupMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalChatChangeType.UPDATE.equalsIgnoreCase(changeType)){
                copGroupMqMsgEventHandler.updateGroupMsgEvent(mqParam, extMap);
            }
            if(WxCpConsts.ExternalChatChangeType.DISMISS.equalsIgnoreCase(changeType)){
                copGroupMqMsgEventHandler.dismissGroupMsgEvent(mqParam, extMap);
            }
        }
        if(WxCpConsts.EventType.MSGAUDIT_NOTIFY.equalsIgnoreCase(event)){
            copDataTalkMqMsgEventHandler.notifySyncMsgEvent(mqParam, extMap);
        }
        return false;
    }

}

